package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * OpenConf.java
 * <p>
 * 
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0
 * @Date Jan 8, 2010
 */
public class OpenConf extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(OpenConf.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql = "select c1.name name, c1.comment comment, c2.defvalue defvalue,c2.value2 value2 " + "from master.dbo.sysconfigures c1, master.dbo.syscurconfigs c2 where c1.config = c2.config " + "and c1.name in ('number of open databases', 'number of open objects', 'number of open indexes')";
            String[] rs = this.getStrings(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase open config", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[6];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql);
            int openDBsNum = -1;
            int confDBsNum = -1;
            int openObjsNum = -1;
            int confObjsNum = -1;
            int openIndexsNum = -1;
            int confIndexsNum = -1;
            while (rst.next()) {
                String name = rst.getString("name");
                String defValue = rst.getString("defvalue");
                String value2 = rst.getString("value2");
                name = name == null ? "" : name.trim();
                defValue = defValue == null ? "" : defValue.trim();
                value2 = value2 == null ? "" : value2.trim();

                if (name.matches("number of open databases")) {
                    openDBsNum = Integer.parseInt(value2);
                    confDBsNum = Integer.parseInt(defValue);
                } else if (name.matches("number of open objects")) {
                    openObjsNum = Integer.parseInt(value2);
                    confObjsNum = Integer.parseInt(defValue);
                } else if (name.matches("number of open indexes")) {
                    openIndexsNum = Integer.parseInt(value2);
                    confIndexsNum = Integer.parseInt(defValue);
                }
            }
            propValArr[0] = openDBsNum + "";
            propValArr[1] = confDBsNum + "";
            propValArr[2] = openObjsNum + "";
            propValArr[3] = confObjsNum + "";
            propValArr[4] = openIndexsNum + "";
            propValArr[5] = confIndexsNum + "";

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "打开的DB数", "配置的DB数", "打开的Object数", "配置的Object数", "打开的Index数", "配置的Index数" };
        return new String[] { "OpenedDBNum", "ConfiguredDBNum", "OpenedObjectNum", "ConfiguredObjectNum", "OpenedIndexNum", "ConfiguredIndexNum" };
    }
}
